/*
 * Copyright 2003-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.tsinghua.cic.ccss.web;

import java.util.List;

import javax.annotation.Resource;

import org.forten.utils.security.TripletsDESUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.tsinghua.cic.ccss.SystemConfig;
import cn.edu.tsinghua.cic.ccss.bo.SystemSettingBo;
import cn.edu.tsinghua.cic.ccss.dto.SystemSettingDto;

/**
 * 
 * 
 * @author <a href="mailto:du_yi@bbn.cn">Duyi</a>
 * @since 2012-5-16
 */
@Controller
@Scope("prototype")
@RequestMapping("setting")
public class SystemSettingAction extends SuperAction {
	@Resource(name = "systemSettingBo")
	private SystemSettingBo systemSettingBo;
	private String message = null;

	@RequestMapping("gotoList")
	public String gotoList(Model model) {
		List<SystemSettingDto> list = systemSettingBo.loadAll();
		for (SystemSettingDto dto : list) {
			if (dto.getId().equals("EMAIL_PASSWORD")) {
				dto.setValue(TripletsDESUtil.decrypt(SystemConfig.KEY,
						dto.getValue()));
			}
		}
		model.addAttribute("dtoList", list);
		return "setting/setting";
	}

	@RequestMapping("update")
	public String update(Model model, @RequestParam String[] id,
			@RequestParam String[] value) {
		for (int i = 0; i < id.length; i++) {
			if (id[i].equals("EMAIL_PASSWORD")) {
				value[i] = TripletsDESUtil.encrypt(SystemConfig.KEY, value[i]);
			}
			systemSettingBo.update(id[i], value[i]);
		}
		message = "系统参数修改成功";
		model.addAttribute("message", message);
		return "forward:gotoList.jspx";
	}

	// @ReinitSystemSetting
	// @DicReload(dicName = "systemDic", loadBeanName =
	// "simpleDicLoaderForJDBC")
	// @RequestMapping("encryptDate")
	// public String changeTripletsDESKey(Model model) {
	// cadreBo.encryptDataWithNewKey();
	// message = "干部信息重新加密完成";
	// model.addAttribute("message", message);
	// return "forward:gotoList.jspx";
	// }
	// 以下是setting页面中的见容
	// <form action="encryptDate.jspx" name="frm"
	// onsubmit="document.getElementById('but').disabled=true">
	// <input type="button" id='but' value="生成新密钥并重新加密干部数据" class="button"
	// onclick="doSubmit();"/>
	// </form>
}
